package com.fingard.dsp.bank.directbank.xib01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;

import java.util.List;

/**
 * Created by dalt on 2017/7/10.
 */
public class XIB01Balance extends XIB01Base implements IBalance {
    String txnCode = "";
    String signdata = "";
    String bankRet = "";

    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        RetAts7506BalQry Resp = new RetAts7506BalQry(reqHeader);
        Resp.writeStart();
        txnCode = "IESI109001";
        String clientReq = writeHeader(txnCode);
        p_req7506.readRecord();
        
        String accountNo = p_req7506.readedDetail.actNum;
        clientReq += "{\"accountNo\":\"" + accountNo + "\"}";
        clientReq += "}";
        signdata = Signatures(clientReq);
        bankRet = postData(signdata, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode,  respMsg))
        {
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
             Resp.writeEnd();return Resp;
        }
        JSONObject body = bankResp.getJSONObject("body");
        RetAtsBalDetail detail = new RetAtsBalDetail();
        detail.respCode = "0";
        detail.respInfo = respMsg;
        detail.actNum = p_req7506.readedDetail.actNum;
        detail.availBal = body.getString("onlineActualBal");
        detail.currBal = body.getString("acctBal");
        Resp.writeRetDetail(detail, false);

         Resp.writeEnd();return Resp;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry resp = new RetAts7505HisBalQry(reqHeader);
        resp.writeStart();
        txnCode = "IESI109020";
        String clientReq = writeHeader(txnCode);
        clientReq += "{\"accountNo\":\"" + p_req7505.actNum + "\",";
        clientReq += "\"startDate\":\"" + p_req7505.dateFrom + "\",";
        clientReq += "\"endDate\":\"" + p_req7505.dateTo + "\"}";
        clientReq += "}";
        signdata = Signatures(clientReq);
        bankRet = postData(signdata, bankFront.serverIP, bankFront.serverPort);
        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode,  respMsg))
        {
            resp.writeRespCode("-1");
            resp.writeRespInfo(respMsg);
            resp.writeEnd();
            return resp;
        }
        JSONObject body = bankResp.getJSONObject("body");
        List<Object> list = body.getJSONArray("balanceInfoList");
        if (list == null || list.size() == 0) {
            resp.writeRespCode("0");
            resp.writeRespInfo("无历史余额返回！");
            resp.writeEnd();
            return resp;
        }
        for (Object iter : list) {
            JSONObject i = (JSONObject) iter;
            RetAtsHisBalDetail detail = new RetAtsHisBalDetail();
            String balDate = i.getString("balDate");//余额日期
            detail.balDate = balDate.substring(0, 4) + "-" + balDate.substring(4, 6) + "-" +balDate.substring(6, 8);
            detail.availBal = i.getString("acctBal");//历史可用余额
            detail.actBal = i.getString("acctBal");//历史余额
            resp.writeRetDetail(detail);
        }
        resp.writeEnd();
        return resp;
    }
}
